<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<link rel="stylesheet" href="/lib/layui/css/layui.css" media="all">
<link rel="stylesheet" href="/css/public.css" media="all">
<script>
    let d1 = "";

    function child(data){

        console.log("父类进来了"+data.funcName);
        $("[name='funcName']").val(data.funcName);
        $("[name='funcCode']").val(data.funcCode);
        $("[name='funcUrl']").val(data.funcUrl);

        $("[name='hidden']").val(data.parentId);

        d1 = data.parentId;

        return data;
    }
</script>
<body>
<form class="layui-form" lay-filter="funcForm">
    <div class="layui-form-item">
        <label class="layui-form-label">权限名称
            <span style="color: red">*</span>
        </label>
        <div class="layui-input-block">
            <input type="text" name="funcName" lay-verify="required" autocomplete="off" placeholder="请输入权限名称"
                   class="layui-input">
        </div>

        <label class="layui-form-label">权限标识
            <span style="color: red">*</span>
        </label>
        <div class="layui-input-block">
            <input type="text" name="funcCode" lay-verify="required" autocomplete="off" placeholder="请输入权限标识"
                   class="layui-input">
        </div>

        <label class="layui-form-label">菜单url
            <span style="color: red">*</span>
        </label>
        <div class="layui-input-block">
            <input type="text" name="funcUrl" lay-verify="required" autocomplete="off" placeholder="请输入菜单url"
                   class="layui-input">
        </div>

        <div class="layui-inline">
            <label class="layui-form-label">上级菜单<span style="color: red">*</span>
            </label>
            <div class="layui-input-inline">
                <select id="modules" name="parentId" lay-verify="required" lay-search="">
                    <option value=""></option>
                </select>
            </div>
        </div>
    </div>

    <input type="hidden" name="hidden" id="hidden">

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
</body>
<script src="/lib/jquery-3.4.1/jquery-3.4.1.min.js"></script>
<script src="/lib/layui/layui.js" charset="utf-8"></script>
<script src="/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script>



    layui.use(['form', 'jquery', 'layedit', 'laydate'], function () {


        var form = layui.form
            , $ = layui.jquery
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate;

        $.ajax({
            url: 'http://192.168.100.222:8082/role/findAllfunc.do',
            type: "post",
            dataType: "json",
            data:{roleId:null},
            success: function (rec) {
                if (rec.code !== 0) {
                    layer.msg("初始化数据失败", {icon: 5, time: 1500})
                } else {
                    $('#modules').empty();
                    $('#modules').append(new Option("请选择上级权限菜单", ""));
                    $.each(rec.data, function (index, item) {
                        let hiId = $("[name='hidden']").val();
                        let parentId = item.parentId;
                        console.log(parentId);
                        console.log(d1);
                        //console.log(child());


                        $('#modules').append(new Option(item.funcName, item.id,null,item.id===d1));// 下拉菜单里添加元素

                    });
                    form.render("select");
                }
            }
        });
        //监听提交
        form.on('submit(demo1)', function (data) {
            console.log(data.field);
            let val = form.val("funcForm");
            console.log(val);
            $.ajax({
                url: "http://192.168.100.222:8082/role/addFunc.do",
                type: "post",
                data: data.field,
                dataType: "json",
                success: function (rec) {
                    if (rec.code === 0) {
                        layer.msg("添加成功", {icon: 6, time: 1000},function () {
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            parent.layer.close(index);
                        });
                        return false;
                    } else {
                        layer.msg("添加失败", {icon: 5, time: 1000});
                        return false;
                    }
                }
            });
            return false;
        });
    })
</script>
</html>